/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-19
 * Time: 12:36
 */
public class MyCircularQueue {
    public int[] elem;
    public int front;
    public int rear;
    public MyCircularQueue(int k) {
        elem = new int[k + 1]; //多开一个空间，因为我们要预留一个空间来判断循环队列是否为空
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean deQueue() {  //从队头出元素
        if(isEmpty()){
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = -1;
        if(rear == 0){  //这里为0就是因为刚刚队列满了，rear第二次循环从头开始，实际要获取的是队尾元素
            index = elem.length - 1;
        }else{
            index = rear - 1;   //-1是因为队尾标志空位置，实际队尾元素要减1
        }
        return elem[index];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
       return (rear + 1) % elem.length == front; //不能直接写成rear + 1 == elem.legnth , read是会变成0和1的
    }
}
